package com.example.actearly_revised;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.TabActivity;
import android.content.res.Resources;
import android.graphics.Typeface;

//@SuppressWarnings("deprecation")
public class QuicklistTabHomePage extends TabActivity {

	private TabHost mTabHost;
	private HashMap mapTabInfo = new HashMap();
	public TabInfo mLastTab = null;
	public SQLogic entry;
	Typeface customFont;
	Intent intent;
	View actionbarView;
	public static int currentmilestone = 0;
	static User[] users;

	TabHost th;
	String [] spinnerItems= new String[]{
			"Quick List",
			"Milestone Tracker",
			"Add/Edit Child Profile"

	};


	private class TabInfo {
		private String tag;
		private Class clss;
		private Bundle args;
		private Fragment fragment;
		TabInfo(String tag, Class clazz, Bundle args) {
			this.tag = tag;
			this.clss = clazz;
			this.args = args;

		}
	}

	class TabFactory implements TabContentFactory {

		private final Context mContext;

		/**
		 * @param context
		 */
		public TabFactory(Context context) {
			mContext = context;
		}

		public View createTabContent(String tag) {
			View v = new View(mContext);
			v.setMinimumWidth(0);
			v.setMinimumHeight(0);
			return v;
		}

	}


	@SuppressLint({ "NewApi", "NewApi" })
	@Override
	public void onCreate(Bundle savedInstanceState) {





		super.onCreate(savedInstanceState);
		//change font/color/text in action bar
		customFont = Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");

		getActionBar().setTitle("What most babies do at this age");

		//need to check if person does or doesn't have a profile
		SQLogic conn = new SQLogic(QuicklistTabHomePage.this);
		conn.open();
		users = conn.getAllUserData();
		conn.close();

		//create the tabhost and set the default page as Milestone Tracker
		//also, define the tab names here
		//	super.onCreate(savedInstanceState);

		th = getTabHost();
		Intent i;
		i = new Intent().setClass(this, QuicklistPage.class);
		// need to make this text consistent with size/font of the app
		TextView tabText = new TextView(this);
		tabText.setText("Milestones");
		tabText.setPadding(8,12,8,12);
		tabText.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		tabText.setTypeface(customFont);

		//this sets the text in the tab according to the specs defined above
		TabSpec sp = th.newTabSpec("Tab1");
		sp.setIndicator(tabText);
		sp.setContent(i);
		th.addTab(sp);


		//  Intent i;
		i = new Intent().setClass(this, UrgentlistPage.class);
		TextView tabText2 = new TextView(this);
		tabText2.setText("Concern List  ");
		tabText.setPadding(8,9,8,9);
		tabText2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		tabText2.setTypeface(customFont);

		//this sets the text in the tab according to the specs defined above
		sp = th.newTabSpec("Tab2");
		sp.setIndicator(tabText2);
		sp.setContent(i);
		th.addTab(sp);

		i = new Intent().setClass(this, HelpfulHints.class);
		TextView tabText3 = new TextView(this);
		tabText3.setText("Helpful Hints");
		tabText.setPadding(8,9,8,9);
		tabText3.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		tabText3.setTypeface(customFont);

		//this sets the text in the tab according to the specs defined above
		sp = th.newTabSpec("Tab 3");
		sp.setIndicator(tabText3);
		sp.setContent(i);
		th.addTab(sp);

		//make tabs bigger!
		Display display = getWindowManager().getDefaultDisplay();
		int height = display.getHeight();
		int width= display.getWidth();
		th.getTabWidget().getChildAt(0).setLayoutParams
		(new LinearLayout.LayoutParams(width/3,(height/14)));

		th.setOnTabChangedListener(new OnTabChangeListener() {
			public void onTabChanged(String arg0) {

				setTabColor(th);
				if (th.getCurrentTab()==0)
				{
					getActionBar().setTitle("What most babies do at this age");
				}
				else if (th.getCurrentTab()==1)
				{					
					getActionBar().setTitle("Talk to your doctor if your child:");
				}
				else if (th.getCurrentTab()==2)
				{					
					getActionBar().setTitle("Activities to do with your child:");

				}
			}
		});
		setTabColor(th);
	}

	//Change The Background Color of Tabs
	public void setTabColor(TabHost tabhost) {

		for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
			tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Integer.parseInt("FFFFFF", 16)+0xFF000000); //unselected

		if(tabhost.getCurrentTab()==0)
			tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Integer.parseInt("FFEDDB", 16)+0xFF000000); //1st tab selected
		else
			tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Integer.parseInt("FFEDDB", 16)+0xFF000000); //2nd tab selected
	}

	//lets you change tabs within activities
	public void switchTab(int tab){
		th.setCurrentTab(tab);
	}


	@Override
	//public boolean onOptionsItemSelected(MenuItem item) {
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.actionbar_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}



	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.editprofilepage)
		{			
			if (users.length == 0){
				Intent intent = new Intent(QuicklistTabHomePage.this, LoginView.class);
				intent.putExtra("NoUsers", true);
				intent.putExtra("ModeSelected", "AddEditProfile");
				startActivity(intent);
			}

			else
			{
				intent = new Intent(QuicklistTabHomePage.this, PickProfile.class);
				intent.putExtra("NoUsers", false);
				intent.putExtra("ModeSelected", "AddEditProfile");
				startActivity(intent);	
			}
		}
		if (item.getItemId() == R.id.milestonepage)
		{			
			// do nothing
			if(users.length == 0){
				//if user tries to change profile but doesn't actually have a profile
				Context context = getApplicationContext();
				CharSequence text = "No profile exists!Please select 'Add/Edit Child Profile' to create one";
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}
			else
			{
				Intent intent = new Intent(QuicklistTabHomePage.this, PickProfile.class);
				intent.putExtra("NoUsers", false);
				intent.putExtra("ModeSelected", "SelectProfile");
				startActivity(intent); // Later, this will go to a screen where they either pick a current profile
				// or add a new one
			}			
		}
		if (item.getItemId() == R.id.quicklistpage)
		{			
			th.setCurrentTab(0);
		}

		if (item.getItemId() == R.id.changeprofilepage)
		{	


			if(users.length == 0){
				//if user tries to change profile but doesn't actually have a profile
				Context context = getApplicationContext();
				CharSequence text = "No profile exists!Please select 'Add/Edit Child Profile' to create one";
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
				/*
				Intent intent = new Intent(QuicklistTabHomePage.this, LoginView.class);
				intent.putExtra("NoUsers", true);
				intent.putExtra("ModeSelected", "SelectProfile");
				startActivity(intent);*/
			}
			else
			{
				Intent intent = new Intent(QuicklistTabHomePage.this, PickProfile.class);
				intent.putExtra("NoUsers", false);
				intent.putExtra("ModeSelected", "SelectProfile");
				startActivity(intent); // Later, this will go to a screen where they either pick a current profile
				// or add a new one
			}

		}

		return super.onOptionsItemSelected(item);
	}




}